
package com.limoilux.cobolux.ui.text;

import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;

import org.eclipse.cobol.ui.common.COBOLKeywordCodasyl88;
import org.eclipse.jface.text.TextAttribute;
import org.eclipse.jface.text.rules.EndOfLineRule;
import org.eclipse.jface.text.rules.IRule;
import org.eclipse.jface.text.rules.IToken;
import org.eclipse.jface.text.rules.MultiLineRule;
import org.eclipse.jface.text.rules.RuleBasedScanner;
import org.eclipse.jface.text.rules.Token;
import org.eclipse.swt.graphics.Color;

import org.eclipse.swt.widgets.Display;

import com.limoilux.cobolux.ui.CoboluxUiUtils;
import com.limoilux.cobolux.ui.editor.CobolEditor;

public class CobolTokenScanner extends RuleBasedScanner
{
	public final String[] reservedWords;
	public final String[] figurativeConstants;
	public final String[] specialRegisters;

	public CobolTokenScanner()
	{
		this.reservedWords = COBOLKeywordCodasyl88.getReservedWords();
		this.figurativeConstants = COBOLKeywordCodasyl88.getFigurativeConstants();
		this.specialRegisters = COBOLKeywordCodasyl88.getSpecialRegisters();

		this.createRules();
	}

	private void createRules()
	{
		List<IRule> ruleList = null;
		ListIterator<IRule> ite = null;
		IRule[] rules = null;
		IRule rule = null;
		int i = 0;

		ruleList = new ArrayList<IRule>();

		this.buildRules(ruleList);

		rules = new IRule[ruleList.size()];
		ite = ruleList.listIterator();
		while (ite.hasNext())
		{
			rule = ite.next();
			rules[i] = rule;
			i++;
		}

		this.setRules(rules);
	}

	private void buildRules(List<IRule> ruleList)
	{
		Display display = null;
		Color color = null;
		TextAttribute ta = null;
		CobolWordRule wordRule = null;
		EndOfLineRule commentRule = null;
		MultiLineRule stringRule = null;
		IToken defaultToken = null;
		IToken reservedWordsToken = null;
		IToken figurativeToken = null;
		IToken registerToken = null;
		IToken commentToken = null;
		IToken stringToken = null;

		display = CoboluxUiUtils.getDisplay();

		color = new Color(display, CobolEditor.COLOR_DEFAULT);
		ta = new TextAttribute(color);
		defaultToken = new Token(ta);

		color = new Color(display, CobolEditor.COLOR_COMMENT);
		ta = new TextAttribute(color);
		commentToken = new Token(ta);

		color = new Color(display, CobolEditor.COLOR_RESERVED);
		ta = new TextAttribute(color, null, 1);
		reservedWordsToken = new Token(ta);

		color = new Color(display, CobolEditor.COLOR_FIGURATIVE);
		ta = new TextAttribute(color, null, 1);
		figurativeToken = new Token(ta);

		color = new Color(display, CobolEditor.COLOR_REGISTER);
		ta = new TextAttribute(color, null, 1);
		registerToken = new Token(ta);

		color = new Color(display, CobolEditor.COLOR_STRING);
		ta = new TextAttribute(color);
		stringToken = new Token(ta);

		commentRule = new CobolCommentRule(commentToken);

		wordRule = new CobolWordRule(defaultToken);
		wordRule.addWords(this.reservedWords, reservedWordsToken);
		wordRule.addWords(this.figurativeConstants, figurativeToken);
		wordRule.addWords(this.specialRegisters, registerToken);

		stringRule = new MultiLineRule("\"", "\"", stringToken);

		ruleList.add(commentRule);
		ruleList.add(wordRule);
		ruleList.add(stringRule);
	}
}
